<template>
  <div class="persional">
      <div class="persional-head"><i class="icon iconfont icon-ren"></i>修改密码</div>
      <div class="resetPassword">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
          <el-form-item label="原密码" class="rolelabel" prop="psd">
            <el-input class="role-select-ipt full-width" type="password" ref="psd" v-model="ruleForm2.psd"></el-input>
          </el-form-item>
          <el-form-item label="新密码" class="rolelabel" prop="pass">
            <el-input class="role-select-ipt full-width" type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" class="rolelabel" prop="checkPass">
            <el-input class="role-select-ipt full-width" type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="select-submit" type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            <el-button class="colse" @click="resetForm('ruleForm2')">重置</el-button>
          </el-form-item>
        </el-form>
          <!-- <el-row :gutter="20">
            <el-col :span="20" :offset="2">
                <el-form label-position="left" label-width="80px" class="aaa">
                    
                </el-form>
            </el-col>
            <el-col :span="20" :offset="2">
                <el-form label-position="left" label-width="80px" class="aaa">
                    <el-form-item label="新密码" class="rolelabel">
                    <el-input class="role-select-ipt full-width" v-model="newPsd" type="password"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="20" :offset="2">
                <el-form label-position="left" label-width="80px">
                    <el-form-item label="确认密码" class="rolelabel">
                    <el-input class="role-select-ipt full-width" v-model="newYPsd" type="password"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="18" :offset="3">
                <el-button @click="closeLayer" class="colse">取    消</el-button>
                <el-button class="select-submit">保    存</el-button>
            </el-col>
        </el-row>       -->
      </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "persional",
  data() {
    var validatePsd = (rule, value, callback) => {//前台验证原密码是否正确
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else if(value != appDB.get("userInfo").psd){
        callback(new Error("原密码输入错误"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请确认新密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        name: "",
        imageUrl: "",
        psd: "",
        pass: "",
        checkPass: "",
        age: ""
      },
      rules2: {
        psd: [{ validator: validatePsd, trigger: "blur" }],
        // psd: [{ required: true, message: "请输入原密码", trigger: 'blur' }],
        pass: [{ validator: validatePass, trigger: "blur" },{ min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  mounted: function() {
    this.name = appDB.get("userInfo").userName;
  },
  methods: {
    //关闭layer弹窗
    closeLayer() {
      layer.closeAll();
      $(".user-name").attr("readonly");
    },
    //验证原密码输入是否正确
    // validPsd() {
    //   axios
    //     .get(
    //       URL +
    //         "/api/userservice/v1/users/" +
    //         appDB.get("userInfo").userBh +
    //         "/" +
    //         this.ruleForm2.psd,
    //       {
    //         headers: { Authorization: appDB.get("userInfo").token }
    //       }
    //     )
    //     .then(res => {
    //       let d = res.data;
    //       this.resultCode = d.resultCode;
    //       if (d.resultCode != 0) {
    //         layer.msg(d.msg);
    //       }
    //     })
    //     .catch(error => {
    //       layer.msg("网络错误");
    //     });
    // },
    //修改密码
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios
            .patch(
              usersURL + '/' + appDB.get("userInfo").userBh,{
                password: this.ruleForm2.pass,
              },
              {
                headers: { Authorization: appDB.get("userInfo").token }
              }
            )
            .then(res => {
              let d = res.data;
              if (d.resultCode == 0) {
                this.closeLayer();
                layer.msg(d.msg);
              } else {
                layer.msg(d.msg);
              }
            })
            .catch(error => {
              layer.msg("网络错误");
            });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    //reset
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
@import "../assets/css/elementUI.css";
.editname {
  width: 116px;
  position: absolute;
  left: 50%;
  margin-left: -50px;
}
.icon-bianji {
  color: #fff;
  cursor: pointer;
  float: left;
  margin-top: 20px;
}
.persional {
  width: 100%;
  float: left;
  background: #072e4a;
}
.persional-head {
  width: 100%;
  float: left;
  height: 30px;
  line-height: 30px;
  padding: 10px 0;
  background: url("../../static/images/line.jpg") repeat-x bottom;
  color: #fff;
  font-size: 16px;
}
.icon-ren {
  float: left;
  color: #fff;
  margin: 0 5px 0 15px;
  font-size: 24px;
}
.icon-guanbi {
  float: right;
  color: #fff;
  margin: 0 15px;
  cursor: pointer;
  font-size: 24px;
}
.persional-upload {
  width: 90%;
  margin: 24px 5%;
  float: left;
}
.resetPassword {
  width: 90%;
  margin: 24px 5%;
  float: left;
}
.resetPassword-head {
  width: 100%;
  float: left;
  border-bottom: 1px solid #45779c;
  color: #fff;
  padding: 5px 0;
  margin-bottom: 50px;
}
.icon-mima {
  margin-right: 5px;
  font-size: 20px;
}
.el-form--label-left .el-form-item__label {
  text-align: left;
}
.colse {
  border-radius: 30px;
  padding: 8px 40px;
  background: none;
  border: 1px solid #367fb6;
  color: #fff;
  margin: 19px 0;
}
.select-submit {
  border-radius: 30px;
  padding: 8px 40px;
  background: #39b423;
  border: 1px solid #39b423;
  color: #fff;
  float: right;
  margin: 19px 0;
}
.user-name {
  background: none;
  width: 100px;
  margin: 0 auto;
  display: block;
  margin-top: 20px;
  color: #fff;
  font-size: 16px;
  text-align: center;
  float: left;
}
</style>


